﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using VNet.VisiSuite.Common.Security;
using VNet.VisiSuite.Service.Contact.Message.Base;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.Service.Proxy;
using VNet.VisiSuite.VisiAdmin.Models;

namespace VNet.VisiSuite.VisiAdmin.Providers
{
    public class UserProvider
    {
        public static void GetUsers(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                    };
                    e.Result = client.GetUsers(request);
                }
            }
        }

        public static void InsertUser(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var user = e.Argument as UserLoginItem;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<UserLoginItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = user,
                    };
                    e.Result = client.InsertUser(request);
                }
            }
        }

        public static void UpdateUser(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var user = e.Argument as UserLoginItem;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<UserLoginItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = user,
                    };
                    e.Result = client.UpdateUser(request);
                }
            }
        }

        public static void DeleteUser(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;
            if (worker != null && worker.CancellationPending == false)
            {
                var user_login_id = (int)e.Argument;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new DeleteDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        DataId = user_login_id,
                    };
                    e.Result = client.DeleteUser(request);
                }
            }
        }
    }
}
